$(function() {

    var provinceId, cityId, districtId, dealersId; 
    var channelId = '30071';

    $.ajax({
        //async:false,
		type: "GET",
		//url: 'http://mediacommons.skoda-1.com/skoda_mediacommons/provinces.interface',
        url: 'http://mediacommons.eskoda.com.cn/skoda_mediacommons/provinces.interface',
		dataType: 'JSONP',
        jsonp: 'jsoncallback',
        data: {},
		success: function(data) {
            $("#provincesTemplate").tmpl(data).appendTo("#provincesSelect");
		},
		error: function(e) {
			
		}
	});

    $('#provincesSelect').on('change', function(){
        provinceId = $(this).val();

        $.ajax({
            //async:false,
	    	type: "GET",
	    	//url: 'http://mediacommons.skoda-1.com/skoda_mediacommons/citys.interface',
            url: 'http://mediacommons.eskoda.com.cn/skoda_mediacommons/citys.interface',
	    	dataType: 'JSONP',
            jsonp: 'jsoncallback',
            data: {'provinceid':provinceId},
	    	success: function(data) {
                $('#citysSelect').html('');
                $('<option value="">--请选择--</option>').appendTo("#citysSelect");
                $('#districtsSelect').html('');
                $('<option value="">--请选择--</option>').appendTo("#districtsSelect");
                $('#dealersSelect').html('');
                $('<option value="">--请选择--</option>').appendTo("#dealersSelect");
                $("#citysTemplate").tmpl(data).appendTo("#citysSelect");
	    	},
	    	error: function(e) {
	    		
	    	}
	    });
    });

    $('#citysSelect').on('change', function(){
        cityId = $(this).val();

        $.ajax({
            //async:false,
	    	type: "GET",
	    	//url: 'http://mediacommons.skoda-1.com/skoda_mediacommons/districts.interface',
            url: 'http://mediacommons.eskoda.com.cn/skoda_mediacommons/districts.interface',
	    	dataType: 'JSONP',
            jsonp: 'jsoncallback',
            data: {'cityid':cityId},
	    	success: function(data) {
                $('#districtsSelect').html('');
                $('<option value="">--请选择--</option>').appendTo("#districtsSelect");
                $('#dealersSelect').html('');
                $('<option value="">--请选择--</option>').appendTo("#dealersSelect");
                $("#districtsTemplate").tmpl(data).appendTo("#districtsSelect");
	    	},
	    	error: function(e) {
	    		
	    	}
	    });

        $.ajax({
            //async:false,
		    type: "GET",
		    //url: 'http://mediacommons.skoda-1.com/skoda_mediacommons/dealers.interface',
            url: 'http://mediacommons.eskoda.com.cn/skoda_mediacommons/dealers.interface',
		    dataType: 'JSONP',
            jsonp: 'jsoncallback',
            data: {'cityid':cityId},
		    success: function(data) {
                $('#dealersSelect').html('');
                $('<option value="">--请选择--</option>').appendTo("#dealersSelect");
                $("#dealersTemplate").tmpl(data).appendTo("#dealersSelect");
		    },
		    error: function(e) {
		    	
		    }
	    });
    });


    $('#districtsSelect').on('change', function(){
        districtId = $(this).val();

        $.ajax({
            //async:false,
		    type: "GET",
		    //url: 'http://mediacommons.skoda-1.com/skoda_mediacommons/dealers.interface',
            url: 'http://mediacommons.eskoda.com.cn/skoda_mediacommons/dealers.interface',
		    dataType: 'JSONP',
            jsonp: 'jsoncallback',
            data: {'cityid':cityId,'districtid':districtId},
		    success: function(data) {
                $('#dealersSelect').html('');
                $('<option value="">--请选择--</option>').appendTo("#dealersSelect");
                $("#dealersTemplate").tmpl(data).appendTo("#dealersSelect");
		    },
		    error: function(e) {
		    	
		    }
	    });
    });

    $('#dealersSelect').on('change', function(){
        dealersId = $(this).val();

    });


    $('#submit').on('click', function(){
        var name = $('#name').val();
        if(name == ""){
            alert('请填写姓名');
            return false;
        }
        var mobile = $('#mobile').val();
        var reg = /^1\d{10}$/;
        if (!reg.test(mobile)) {
            alert('请正确填写手机号');
            return false;
        }
        var s=s_gi(s_account);
        s.linkTrackVars="events";
        s.linkTrackEvents="event8";
        s.events="event8";
        s.tl(this,'o','custom');
        $.ajax({
            //async:false,
		    type: "GET",
		    //url: 'http://mediacommons.skoda-1.com/skoda_mediacommons/processleads/callback/submitclue.interface',
            url: 'http://mediacommons.eskoda.com.cn/skoda_mediacommons/processleads/callback/submitclue.interface',
		    dataType: 'JSONP',
            jsonp: 'jsoncallback',
            data: {'channelid':channelId,'realname':name,'mobile':mobile,'provinceid':provinceId,'cityid':cityId,'districtid':districtId,'dealerid':dealersId,'vehicleseriesid':'122'},
            beforeSend: function(){
                console.log({'channelid':channelId,'realname':name,'mobile':mobile,'provinceid':provinceId,'cityid':cityId,'districtid':districtId,'dealerid':dealersId,'vehicleseriesid':'122'});
            },
		    success: function(data) {
                if(data.result_code == '200'){
                    alert('提交成功')
                }
		    },
		    error: function(e) {
		    	//alert(e.status)
		    }
	    }); 
    });



})

